<template>
  <div class="app-container">
    <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
    <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
    </el-table>
  </div>
</template>

<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import { addAllExcelEmployee } from '@/api/employees'
import { formatExcelDate } from '@/utils/index'

export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent },
  data() {
    return {
      tableData: [],
      tableHeader: []
    }
  },
  methods: {
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    // 导入成功后的回调函数
    handleSuccess({ results, header }) {
      this.tableData = results
      this.tableHeader = header
      // console.log('results是啥?', results)
      const data = this.addAllDataExcel(results)
      this.loadAllExcelEmployee(data)
      // console.log('此时的data', data)  //转换后的数据
      // console.log('header是啥?', header)
    },
    // excel 上传的数据格式转换
    addAllDataExcel(data) {
      // 1. 映射对象，根据映射对象找英文的键
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      const results = data.map(item => {
        // console.log(item)
        const header = {}
        const zhkeys = Object.keys(item)
        // ['入职日期', '姓名', '工号', '手机号', '转正日期', '部门']
        // console.log('Object.keys(item)', Object.keys(item))
        zhkeys.forEach(key => {
          // console.log('key', key)
          const enkey = mapInfo[key]
          if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
            // 时间格式 解码  时间格式转换 标准格式
            // console.log('item[key]---------', item[key])
            // formatExcelDate(item[key])
            // console.log( formatExcelDate(item[key]))
            header[enkey] = new Date(formatExcelDate(item[key]))
            // console.log(new Date(formatExcelDate(item[key])))
          } else {
            // console.log('enkey', enkey)
            header[enkey] = item[key]
            // console.log('header', header)
          }
        })
        return header
      })
      return results
    },
    // excel批量导入员工
    async loadAllExcelEmployee(data) {
      try {
        const res = await addAllExcelEmployee(data)
        console.log('addAllExcelEmployee', res)
        // 保存数据
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.$router.back()
      } catch (err) {
        console.log('addAllExcelEmployee', err)
      }
    }
  }
}
</script>
